package fb.bgy;

import android.app.DownloadManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Login_Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_);

        findViewById(R.id.login_btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendRequestWithOKhttp();
                Intent intent=new Intent(Login_Activity.this,Main_Activity.class);
                startActivity(intent);


            }
        });
        findViewById(R.id.login_btn_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Login_Activity.this,"暂时不开放注册功能",Toast.LENGTH_SHORT).show();

            }
        });
        findViewById(R.id.login_text_change_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent intent=new Intent(Login_Activity.this,Uppass.class);
                startActivity(intent);
            }
        });


    }

    //向服务器发送请求
    private void SendRequestWithOKhttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().url("http://warningsystem.ittas.cn/index.php/Home/index/tologin").build();
                    Response response = client.newCall(request).execute();
                    String responseData=response.body().string();
                    parseJsonWithJSONObjcet(responseData);


                }
                catch (Exception e)
                {
                 e.printStackTrace();
                }

            }
        }).start();

    }
    //接受服务器返回jsn数据

    private  void parseJsonWithJSONObjcet( String JsonDate)
    {
        try{
            JSONArray jsonArray=new JSONArray(JsonDate);
            for (int i=0;i<jsonArray.length();i++)
            {
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                String id=jsonObject.getString("id");
                Log.d("Login_Activity",id.toString());

            }

        }
        catch (Exception e){
            e.printStackTrace();
        }


    }
}
